package com.zhitong.money.controller;

import com.github.pagehelper.PageInfo;
import com.zhitong.money.entity.AjaxResponse;
import com.zhitong.money.entity.Holiday;
import com.zhitong.money.exception.RepeatException;
import com.zhitong.money.services.api.IHolidayService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@Slf4j
@RestController
public class HolidayContro {

    @Autowired
    private IHolidayService service;

    @GetMapping("/holidays")
    public AjaxResponse getAllHoliday() throws Exception{
        List<Holiday> allHoliday = service.findAllHoliday();
        return AjaxResponse.success(allHoliday);
    }
    @PostMapping("/holidays")
    public AjaxResponse getHolidayByPage(@RequestBody Map<String,Object> map) throws Exception{
        PageInfo holidayByPage = service.findHolidayByPage(map);
        return AjaxResponse.success(holidayByPage);
    }

    @GetMapping("/holiday/{id}")
    public AjaxResponse findHolidayById(@PathVariable String id) throws Exception{
        Holiday holiday = service.fidHolidayById(id);
        return AjaxResponse.success(holiday);
    }

    @RequiresPermissions({"addHoliday"})
    @PostMapping("/holiday")
    public AjaxResponse addHoliday(@RequestBody Holiday holiday) throws Exception{
        service.addHoliday(holiday);
        return AjaxResponse.success("添加假期成功!");
    }

    @RequiresPermissions({"updateHoliday"})
    @PutMapping("/holiday")
    public AjaxResponse updateHoliday(@RequestBody Holiday holiday) throws Exception{
        service.updateHoliday(holiday);
        return AjaxResponse.success("修改假期成功!");
    }

    @RequiresPermissions({"delHoliday"})
    @DeleteMapping("/holiday/{id}")
    public AjaxResponse delHoliday(@PathVariable("id")String id) throws Exception{
        service.delHoliday(id);
        return AjaxResponse.success("假期删除成功!");
    }

    @RequiresPermissions({"delHoliday"})
    @PostMapping("/delholidays")
    public AjaxResponse delHolidayByIds(@RequestBody List<String> ids) throws Exception{
        service.delHolidayByIds(ids);
        return AjaxResponse.success("删除成功!");
    }

}
